package groupb6.shopsystem.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import groupb6.shopsystem.annotation.JWTPass;
import groupb6.shopsystem.pojo.form.LoginForm;
import groupb6.shopsystem.pojo.form.RegisterNormalForm;
import groupb6.shopsystem.pojo.form.RegisterSellerForm;
import groupb6.shopsystem.pojo.res.TokenRes;
import groupb6.shopsystem.service.IndexService;
import groupb6.shopsystem.util.response.UniversalResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "0:index")
@ApiSupport(author = "chashao")
@RestController
@RequestMapping("/")
public class IndexController {
    @Resource
    private IndexService indexService;

    @ApiOperationSupport(author = "这里写接口负责人")
    @ApiOperation("接口描述")
    @GetMapping
    @JWTPass
    public UniversalResponse<Object> index() {
        return new UniversalResponse<>(1, "OK");
    }

    /**
     * 将登录，注册操作封装到CommonService（impl）接口之中
     * 在登录后，将token和identity传给前端，用以登录时区分跳转到不同用户界面
     * @param loginForm 登录表单
     * @return token,身份
     */
    @ApiOperationSupport(author = "chashao")
    @ApiOperation("登录接口")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "登陆成功"),
            @ApiResponse(code = 2002, message = "账号不存在或密码错误"),
    })
    @PostMapping("/login")
    @JWTPass
    public UniversalResponse<TokenRes> login(@RequestBody LoginForm loginForm) {
        return indexService.login(loginForm);
    }



    @ApiOperationSupport(author = "chashao")
    @ApiOperation("注册普通用户接口")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "注册成功"),
            @ApiResponse(code = 1002, message = "参数为空"),
            @ApiResponse(code = 2003, message = "账户已存在"),
    })
    @PostMapping("/register")
    @JWTPass
    public UniversalResponse<?> register(@RequestBody RegisterNormalForm registerNormalForm) {
        return indexService.register(registerNormalForm);
    }

    @ApiOperationSupport(author = "chashao")
    @ApiOperation("注册商家用户接口")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "注册成功"),
            @ApiResponse(code = 1002, message = "参数为空"),
            @ApiResponse(code = 2003, message = "账户已存在"),
    })
    @PostMapping("/registerseller")
    @JWTPass
    public UniversalResponse<?> registerSeller(@RequestBody RegisterSellerForm registerSellerForm) {
        return indexService.registerSeller(registerSellerForm);
    }
}
